<script setup lang="ts">
import { getMemberOrderAPI } from '@/services/order'
import type { OrderItem, OrderListParams } from '@/types/order'
import { onMounted, ref } from 'vue'
import { OrderState } from '@/services/constants'
import { getPayMockAPI, getPayWxPayMiniPayAPI } from '@/services/pay'

// 获取屏幕边界到安全区域距离
const { safeAreaInsets } = uni.getSystemInfoSync()
//获取父组件传递的参数
const props = defineProps<{
  orderState: number
}>()
// tabs 数据
const orderTabs = ref([
  { orderState: 0, title: '全部' },
  { orderState: 1, title: '待付款' },
  { orderState: 2, title: '待发货' },
  { orderState: 3, title: '待收货' },
  { orderState: 4, title: '待评价' },
])
//请求参数
const pagePramas: Required<OrderListParams> = {
  page: 1,
  pageSize: 5,
  orderState: props.orderState,
}
//支付订单
const onPay = async (id: string) => {
  if (import.meta.env.DEV) {
    //开发环境模拟支付
    const res = await getPayMockAPI({ orderId: id })
  } else {
    //生产环境,真实api
    const res = await getPayWxPayMiniPayAPI({ orderId: id })
  }
  uni.showToast({
    title: '支付成功',
    icon: 'none',
  })
  const order = orderList.value.find((v) => v.id == id) //获取订单
  order!.orderState = OrderState.DaiFaHuo //修改订单状态
}
//获取订单列表
const orderList = ref<OrderItem[]>([])
//是否以否还有更多数据
const finished = ref(false)

const getOrderListData = async () => {
  if (finished.value) {
    return uni.showToast({
      title: '没有更多数据~',
      icon: 'none',
    })
  }
  const res = await getMemberOrderAPI(pagePramas)
  //数组追加
  orderList.value.push(...res.result.items)
  pagePramas.page++
  if (pagePramas.page > res.result.pages) {
    finished.value = true
  }
}

//页面加载
onMounted(() => {
  getOrderListData()
})
</script>
<template>
  <!-- 订单列表 -->
  <scroll-view scroll-y class="orders" @scrolltolower="getOrderListData">
    <view class="card" v-for="item in orderList" :key="item.id">
      <!-- 订单信息 -->
      <view class="status">
        <text class="date">{{ item.createTime }}</text>
        <!-- 订单状态文字 -->
        <text>{{ orderTabs.find((v) => v.orderState == item.orderState)?.title }}</text>
        <!-- 待评价/已完成/已取消 状态: 展示删除订单 -->
        <text class="icon-delete" v-if="item.orderState >= OrderState.DaiPingJia"></text>
      </view>
      <!-- 商品信息，点击商品跳转到订单详情，不是商品详情 -->
      <navigator
        v-for="sku in item.skus"
        :key="sku.id"
        class="goods"
        :url="`/pagesOrder/detail/detail?id=${item.id}`"
        hover-class="none"
      >
        <view class="cover">
          <image mode="aspectFit" :src="sku.image"></image>
        </view>
        <view class="meta">
          <view class="name ellipsis">{{ sku.name }}</view>
          <view class="type">{{ sku.attrsText }}</view>
        </view>
      </navigator>
      <!-- 支付信息 -->
      <view class="payment">
        <text class="quantity">共{{ item.totalNum }}件商品</text>
        <text>实付</text>
        <text class="amount"> <text class="symbol">¥</text>{{ item.payMoney }}</text>
      </view>
      <!-- 订单操作按钮 -->
      <view class="action">
        <!-- 待付款状态：显示去支付按钮 -->
        <template v-if="item.orderState == OrderState.DaiFuKuan">
          <view class="button primary" @tap="onPay(item.id)">去支付</view>
        </template>
        <template v-else>
          <navigator
            class="button secondary"
            :url="`/pagesOrder/create/create?orderId=${item.id}`"
            hover-class="none"
          >
            再次购买
          </navigator>
          <!-- 待收货状态: 展示确认收货 -->
          <view v-if="props.orderState == OrderState.DaiShouHuo" class="button primary"
            >确认收货</view
          >
        </template>
      </view>
    </view>
    <!-- 底部提示文字 -->
    <view class="loading-text" :style="{ paddingBottom: safeAreaInsets?.bottom + 'px' }">
      {{ finished ? '没有更多数据~' : '正在加载...' }}
    </view>
  </scroll-view>
</template>
<style></style>
